// ===============================================
// date_time_utils
//
// Create by Will on 25/3/2024 23:00
// Copyright Will.All rights reserved.
// ===============================================

import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:get/get.dart';

import '../core_kit_localizations.dart';

class CoreDateTimeUtils {
  CoreDateTimeUtils._internal();

  //时间转换
  static const num _oneMinute = 60000;
  static const num _oneHour = 3600000;
  static const num _oneDay = 86400000;

  // static const num _oneWeek = 604800000;

  static final String _oneSecondAgo = CoreKitS.of(Get.context!).secondAgo;
  static final String _oneMinuteAgo = CoreKitS.of(Get.context!).minuteAgo;
  static final String _oneHourAgo = CoreKitS.of(Get.context!).hourAgo;
  static final String _oneDayAgo = CoreKitS.of(Get.context!).dayAgo;

  // static final String _oneMonthAgo = CoreKitS.of(Get.context!).dayAgo;
  // static final String _oneYearAgo = CoreKitS.of(Get.context!).dayAgo;

  // static String formatByString(String date){
  //   final DateTime date =
  //   return format();
  // }

  static String formatByInt(int date) {
    // 将时间戳转换为DateTime对象
    final dateTime = DateTime.fromMillisecondsSinceEpoch(date * 1000);
    return format(dateTime);
  }

  //时间转换
  static String format(DateTime date) {
    final num delta =
        DateTime.now().millisecondsSinceEpoch - date.millisecondsSinceEpoch;

    if (delta < 1 * _oneMinute) {
      final seconds = _toSeconds(delta);
      return (seconds <= 0 ? 1 : seconds).toInt().toString() + _oneSecondAgo;
    }
    if (delta < 60 * _oneMinute) {
      final minutes = _toMinutes(delta);
      return (minutes <= 0 ? 1 : minutes).toInt().toString() + _oneMinuteAgo;
    }
    if (delta < 24 * _oneHour) {
      final hours = _toHours(delta);
      return (hours <= 0 ? 1 : hours).toInt().toString() + _oneHourAgo;
    }
    if (delta < 48 * _oneHour) {
      return CoreKitS.of(Get.context!).yesterdayAgo;
    }
    // if (delta < 30 * _oneDay) {
    //   num days = _toDays(delta);
    //   return (days <= 0 ? 1 : days).toInt().toString() + _oneDayAgo;
    // }
    // if (delta < 12 * 4 * _oneWeek) {
    //   num months = _toMonths(delta);
    //   return (months <= 0 ? 1 : months).toInt().toString() + _oneMonthAgo;
    // } else {
    //   num years = _toYears(delta);
    //   return (years <= 0 ? 1 : years).toInt().toString() + _oneYearAgo;
    // }

    if (delta < 7 * _oneDay) {
      final days = _toDays(delta);
      return (days <= 0 ? 1 : days).toInt().toString() + _oneDayAgo;
    }
    return DateUtil.formatDate(date, format: DateFormats.y_mo_d);
  }

  static num _toSeconds(num date) {
    return date / 1000;
  }

  static num _toMinutes(num date) {
    return _toSeconds(date) / 60;
  }

  static num _toHours(num date) {
    return _toMinutes(date) / 60;
  }

  static num _toDays(num date) {
    return _toHours(date) / 24;
  }

  static num _toMonths(num date) {
    return _toDays(date) / 30;
  }

  static num _toYears(num date) {
    return _toMonths(date) / 12;
  }
}
